package com.ysw.rpc.server.handler.initializer;

import com.ysw.rpc.common.config.MessageCodec;
import com.ysw.rpc.common.config.MessageLengthFieldDecoder;
import com.ysw.rpc.server.config.ServerProperties;
import com.ysw.rpc.server.handler.inbound.RequestHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.concurrent.Executor;

/**
 * @author YinShangwen
 * @since 2023/4/4 14:21
 */
@ChannelHandler.Sharable
public class ServerInitializerHandler extends ChannelInitializer<NioSocketChannel> {
    private final ServerProperties properties;
    private final Executor executor;

    public ServerInitializerHandler(ServerProperties properties, Executor executor) {
        this.properties = properties;
        this.executor = executor;
    }

    @Override
    protected void initChannel(NioSocketChannel ch) {
        ch.pipeline().addLast(new MessageLengthFieldDecoder(), new MessageCodec(properties), new RequestHandler(executor));
    }
}
